﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;
using Lamex.Common;

namespace Lamex.mCost.UI
{
    public partial class frmCOST_HR_Data_Refresh : frmBaseChild
    {
        string strSql;
        DataTable dt;

        public frmCOST_HR_Data_Refresh()
        {
            InitializeComponent();
        }

        private void frmCOST_HR_Data_Refresh_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width / 2;
        }

        private void frmCOST_HR_Data_Refresh_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }

        private void addDataToGridControl(DataTable dt, DevExpress.XtraGrid.GridControl gc, DevExpress.XtraGrid.Views.Grid.GridView gv)
        {
            //if (dt.Rows.Count > 0)
            //{
            gc.DataSource = dt;
            gv.OptionsBehavior.Editable = false;
            gv.OptionsView.ColumnAutoWidth = true;
            //}

            //gridView1页脚汇总设置
            gv.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gv.Columns[0].SummaryItem.DisplayFormat = "计数:";
            gv.Columns[1].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            //gv.Columns[1].SummaryItem.DisplayFormat = "{0:N}";//两位小数
            gv.Columns[1].SummaryItem.DisplayFormat = "{0:N0}";//无小数位
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            DoView(null);//前面不能用加base;
        }

        public override void DoView(IButtonInfo sender)
        {
            //拥有权限者需开通数据库和文件夹操作权限
            if (!(Loginer.CurrentUser.AccountName == "何应书" || Loginer.CurrentUser.AccountName == "陈子锐" || Loginer.CurrentUser.AccountName == "刘邦泉"))
            {
                XtraMessageBox.Show("无权限刷新");
                return;
            }
            importHumanCostData();//刷新人事系统导出的数据进入MES数据库
            refreshHumanData();
            base.DoView(sender);
        }

        //刷新当天更新数据到前端界面
        private void refreshHumanData()
        {
            string strToday = DateTime.Now.AddDays(-31).ToString("yyyy-MM-dd");

            strSql = @"SELECT * FROM Cost_Labor_Comp_Daily
WHERE UPDATE_TIME>='" + strToday + @"'";
            //dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            if (Loginer.CurrentUser.DBName.Contains("测试数据库"))
                dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            else
                dt = Lamex.Common.DataBaseHelper_41.GetDataTableBySql(strSql);
            //dt = Lamex.Common.DataBaseHelper_41.GetDataTableBySql(strSql);

            addDataToGridControl(dt, gridControl1, gridView1);

                strSql = @"SELECT * FROM Cost_Social_Insurance_Monthly
WHERE UPDATE_TIME>='" + strToday + @"'";
            //dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            if (Loginer.CurrentUser.DBName.Contains("测试数据库"))
                dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            else
                dt = Lamex.Common.DataBaseHelper_41.GetDataTableBySql(strSql);
            //dt = Lamex.Common.DataBaseHelper_41.GetDataTableBySql(strSql);
            addDataToGridControl(dt, gridControl2, gridView2);
        }

        //刷新人事系统导出的数据进入MES数据库
        private void importHumanCostData()
        {
            //上线正式数据提取路径：\\tx-fs9\tx-fs\TX-Public\TX-CPHR\HR\MES Comp cost\Daily Comp cost
            //上线前先在[172.23.134.36]测，上线后改为[172.23.134.41]

            /*
            一、每日人力成本数据导入

            1、检查\\Tx-fs10\tx-fs1\LAMEX\MES\Test\Daily Comp cost文件夹中是否有"*.xlsx"文件
            2、如果有则将文件中的数据提取到[172.23.134.36].[Cost_Labor_Comp_Daily]表中COST_DATE,COST_FACTORY
            字段映射如下：
            数据库字段	文件列标或说明
            COST_DATE	计薪日期
            COST_FACTORY	部门名称
            BASIC_SALARY	日出勤工资
            TEMPORARY_COST	临时工工资
            DAILY_BONUS	当日奖金
            OT1_COST	OT1金额
            OT2_COST	OT2金额
            ALLOWANCE	津贴
            LABOR_TIME	生产工时
            NET_PAY_COST	实发工资
            FILE_NAME	数据源文件名
            CREATED_TIME	文件创建时间
            EDITED_TIME	文件修改时间
            UPDATE_TIME	当前系统时间
            UPDATE_USER	当前MES用户
             * 
            二、每日社保公积金数据导入

            1、检查L:\MES\Test\Social Insurance & Housing Provident Fund文件夹中是否有"*.xlsx"文件
            2、如果有则将文件中的数据提取到[172.23.134.36].[Cost_Social_Insurance_Monthly]表中  COST_MONTH,COST_FACTORY           * 
            字段映射如下：
            数据库字段	文件列标或说明
            COST_MONTH	月份
            COST_FACTORY	分厂
            SI_NUMBER	社保人数
            SI_COST	社保公司合计
            HPF_NUMBER	住房公积金人数
            HPF_COST	住房公积金公司合计
            FILE_NAME	数据源文件名
            CREATED_TIME	文件创建时间
            EDITED_TIME	文件修改时间
            UPDATE_TIME	当前系统时间
            UPDATE_USER	当前MES用户
            
            以上做成一个公共方法，供后期其他需求调用


             */

            try
            {
                //In order to build a common method,building about table models make sure field map to Name
                List<Lamex.mCost.Common. FieldNameInfo> costList = new List<Lamex.mCost.Common.FieldNameInfo>();
                costList = new List<Lamex.mCost.Common.FieldNameInfo>()
                {
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="COST_DATE",CNName="计薪日期"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="COST_FACTORY",CNName="部门名称"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="BASIC_SALARY",CNName="日出勤工资"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="TEMPORARY_COST",CNName="临时工工资"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="DAILY_BONUS",CNName="当日奖金"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="OT1_COST",CNName="OT1金额"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="OT2_COST",CNName="OT2金额"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="ALLOWANCE",CNName="津贴"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="LABOR_TIME",CNName="生产工时"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="NET_PAY_COST",CNName="实发工资"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="FILE_NAME",CNName="数据源文件名"},
                    new Lamex.mCost.Common. FieldNameInfo{FieldName="CREATED_TIME",CNName="文件创建时间"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="EDITED_TIME",CNName="文件修改时间"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="UPDATE_TIME",CNName="当前系统时间"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="UPDATE_USER",CNName="当前MES用户"}
                };
                string error1 = "";
                string error2 = "";
                //string strError1 = "";
                if (Loginer.CurrentUser.DBName.Contains("测试数据库"))
                {
                    if (!Lamex.mCost.Common.ExportHelper.FindExcelToDataBase(@"\\Tx-fs10\tx-fs1\LAMEX\MES\Test\Daily Comp cost", @"\\Tx-fs10\tx-fs1\LAMEX\MES\Test\Daily Comp cost\Backup", costList, "Cost_Labor_Comp_Daily", ref error1))
                    {
                        error1 += "每日人力成本保存失败\r\n";
                    }
                    else
                        error1 += "每日人力成本保存成功\r\n";
                }
                else
                {
                    if (!Lamex.mCost.Common.ExportHelper.FindExcelToDataBase(@"\\tx-fs8\tx-fs\TX-Public\TX-CPHR\HR\MES Comp cost\Daily Comp cost", @"\\tx-fs8\tx-fs\TX-Public\TX-CPHR\HR\MES Comp cost\Daily Comp cost\Backup", costList, "Cost_Labor_Comp_Daily", ref error1))
                    {

                        error1 += "每日人力成本保存失败\r\n";
                    }
                    else
                    {
                        if (error1.Contains("无每日人工成本数据"))
                            error1 = "无每日人工成本数据\r\n";
                        else
                            error1 += "每日人力成本保存成功\r\n";
                    }
                }



                List<Lamex.mCost.Common.FieldNameInfo> fundtList = new List<Lamex.mCost.Common.FieldNameInfo>();
                fundtList = new List<Lamex.mCost.Common.FieldNameInfo>()
                {
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="COST_MONTH",CNName="月份"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="COST_FACTORY",CNName="分厂"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="SI_NUMBER",CNName="社保人数"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="SI_COST",CNName="社保公司合计"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="HPF_NUMBER",CNName="住房公积金人数"},
                    new Lamex.mCost.Common. FieldNameInfo{FieldName="HPF_COST",CNName="住房公积金公司合计"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="FILE_NAME",CNName="数据源文件名"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="CREATED_TIME",CNName="文件创建时间"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="EDITED_TIME",CNName="文件修改时间"},
                    new Lamex.mCost.Common. FieldNameInfo{FieldName="UPDATE_TIME",CNName="当前系统时间"},
                    new Lamex.mCost.Common.FieldNameInfo{FieldName="UPDATE_USER",CNName="当前MES用户"}
                };

                if (Loginer.CurrentUser.DBName.Contains("测试数据库"))
                {
                    if (!Lamex.mCost.Common.ExportHelper.FindExcelToDataBase(@"\\Tx-fs10\tx-fs1\LAMEX\MES\Test\Social Insurance & Housing Provident Fund", @"\\Tx-fs10\tx-fs1\LAMEX\MES\Test\Social Insurance & Housing Provident Fund\Backup", fundtList, "Cost_Social_Insurance_Monthly", ref error2))
                    {
                        error2 += "每日社保公积金保存失败";
                    }
                    else
                        error2 += "每日社保公积金保存成功";
                }
                else
                {
                    if (!Lamex.mCost.Common.ExportHelper.FindExcelToDataBase(@"\\tx-fs8\tx-fs\TX-Public\TX-CPHR\HR\MES Comp cost\Social Insurance & Housing Provident Fund", @"\\tx-fs8\tx-fs\TX-Public\TX-CPHR\HR\MES Comp cost\Social Insurance & Housing Provident Fund\Backup", fundtList, "Cost_Social_Insurance_Monthly", ref error2))
                    {
                        error2 += "每日社保公积金保存失败";
                    }
                    else
                    {
                        if (error2.Contains("无社保及公积金数据"))
                            error2 = "无社保及公积金数据";
                        else
                            error2 += "每日社保公积金保存成功";
                    }
                }
                XtraMessageBox.Show(error1+ error2);
                
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}